package com.why.simpleboot.core.server.netty.resolver.impl;

import com.why.simpleboot.core.core.entity.MethodDefinition;
import com.why.simpleboot.core.server.netty.resolver.ParameterResolver;
import com.why.simpleboot.core.server.netty.serialize.JacksonSerializer;
import com.why.simpleboot.core.server.netty.serialize.Serializer;

import java.lang.reflect.Parameter;

/**
 * @program: why-simpleboot-framework
 * @description:
 * @author: @why
 * @create: 2021-05-23 17:30
 **/
public class ReqeustBodyParameterResolver implements ParameterResolver {
    private static final Serializer SERIALIZER = new JacksonSerializer();

    /**
     * @param methodDefinition
     * @param parameter
     * @return
     * @see ParameterResolver
     */
    @Override
    public Object resolve(MethodDefinition methodDefinition, Parameter parameter) {
        String json = methodDefinition.getJson();
        if (json == null) {
            throw new IllegalArgumentException("ReqeustBody解析参数失败");
        }
        return SERIALIZER.deserialize(json.getBytes(), parameter.getType());

    }
}
